import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.log4j.Logger;

public class CreateMultipleProcess implements JavaDelegate {
	private static Logger logger = Logger.getLogger("CreateMultipleProcess");

	@Override
	public void execute(DelegateExecution arg0) throws Exception {
		int count=(Integer)arg0.getVariable("IEcount");
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		final String id=arg0.getProcessInstanceId();
		final RuntimeService runtimeService = processEngine.getRuntimeService();
		ArrayList<Thread> threads = new ArrayList<Thread>();
		for (int i = 0; i < count; i++) {
			final int orderNum=i;
			Thread thread = new Thread() {
				public void run() {		
					Map<String, Object> variables = new HashMap<String, Object>();
					  variables.put("processId", id);
					  variables.put("orderNum", orderNum);
					ProcessInstance processInstance = runtimeService
							.startProcessInstanceByKey("IE",variables);
					logger.info(processInstance.getProcessInstanceId());
				}
			};
			threads.add(thread);
		}
		
		arg0.setVariable("completeIECount", 0);
		arg0.setVariable("lock", Boolean.FALSE);
		
		for (Thread thread : threads) {
			logger.info("Process started");
			thread.start();
		}
		
		
	}

}
